<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('设备类型')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-model="model.type"
                dictCode="bis_device_info.type"
                :placeholder="$t('请选择设备类型')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('版本')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="version">
              <a-input v-model="model.version" :placeholder="$t('请输入版本')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="btnType === 1">
            <a-form-model-item :label="$t('文件名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upFile">
              <j-upload v-model="model.upFile" accept=".upd" :number="1" :uploadAction="importExcelUrl1"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="btnType === 2">
            <a-form-model-item :label="$t('文件名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upFile">
              <j-upload v-model="model.upFile" :number="1" :uploadAction="importExcelUrl2"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BisDeviceUpFileForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        type: [{ required: true, message: this.$t('请输入设备类型') }],
        version: [{ required: true, message: this.$t('请输入版本') }],
        upFile: [{ required: true, message: this.$t('请输入升级文件') }],
      },
      url: {
        add: '/device/bisDeviceUpFile/add',
        edit: '/device/bisDeviceUpFile/edit',
        queryById: '/device/bisDeviceUpFile/queryById',
        importExcelUrl1: '/device/bisDeviceUpFile/uploadDeviceUpFile',
        importExcelUrl2: '/device/bisDeviceUpFile/uploadAppUpFile',
      },
      btnType: 1
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    importExcelUrl1: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl1}`
    },
    importExcelUrl2: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl2}`
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(arg) {
      if (arg == 2) {
        this.modelDefault.type = 'EF000010'
      }
      this.btnType = arg
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          let upFile = that.model.upFile
          let arr = upFile.split(':')
          that.model.upFile = arr[0]
          if(that.btnType === 1){
            that.model.devType = arr[1]
          }
          if(that.btnType === 2){
            that.model.devType = that.model.type
          }
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!that.model.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          httpAction(httpurl, that.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>